﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Threading;

namespace AlertToDo {
  /// <summary>
  /// MainWindow.xaml 的交互逻辑
  /// </summary>
  public partial class MainWindow {
    private readonly DispatcherTimer _timerOnHour = new DispatcherTimer();
    private NotifyIcon _notifyIcon;
    public MainWindow() {
      InitializeComponent();
    }
    private void AlertTimerOnHour(object sender, EventArgs e) {
      //string nowTimeStr1 = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")
      //DateTime.Now.ToLocalTime().ToString()
      string nowTimeStr = DateTime.Now.ToLocalTime().ToString(CultureInfo.InvariantCulture);
      Console.WriteLine("nowTimeStr="+ nowTimeStr);
      // 分钟
      string nowTimeMmStr = DateTime.Now.ToString("mm");
      Console.WriteLine("nowTimeMmStr=" + nowTimeMmStr);
      // 秒
      string nowTimeSsStr = DateTime.Now.ToString("ss");
      Console.WriteLine("nowTimeSsStr=" + nowTimeSsStr);
      NowTime.Content = nowTimeStr;
      if (!nowTimeMmStr.Equals("00") || !nowTimeSsStr.Equals("00")) {
        return;
      }

      //MessageBoxResult result = System.Windows.MessageBox.Show("整点了!" + nowTimeStr, "整点了", 
      //  //
      //  MessageBoxButton.YesNo, System.Windows.MessageBoxOptions.ServiceNotification);
      //if (result == MessageBoxResult.Yes) {
      //  Console.WriteLine(result);
      //}
      //else if (result == MessageBoxResult.No) {
      //  Console.WriteLine(result);
      //}
      Show();
    }
    private void WindowLoad(object sender, RoutedEventArgs eR) {
      _timerOnHour.Interval = TimeSpan.FromSeconds(1);
      _timerOnHour.Tick += AlertTimerOnHour;
      _timerOnHour.Start();
      _notifyIcon = new NotifyIcon();
      _notifyIcon.Text = @"定时任务运行中";
      _notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
      _notifyIcon.Visible = true;
      //打开菜单项
      MenuItem open = new MenuItem("Open");
      open.Click += Show;
      //退出菜单项
      MenuItem exit = new MenuItem("Exit");
      exit.Click += Close;
      //关联托盘控件
      MenuItem[] children = new MenuItem[] { open, exit };
      _notifyIcon.ContextMenu = new ContextMenu(children);

      _notifyIcon.MouseDoubleClick += (o, e) => {
        if (e.Button == MouseButtons.Left) {
          Show(o, e);
        }
      };
    }
    private void Show(object sender, EventArgs e) {
      Visibility = Visibility.Visible;
      ShowInTaskbar = true;
      Activate();
    }
    private void Hide(object sender, EventArgs e) {
      ShowInTaskbar = false;
      Visibility = Visibility.Hidden;
      _notifyIcon.ShowBalloonTip(2000, "提示", "定时任务运行中！", ToolTipIcon.Info);
    }
    private void Close(object sender, EventArgs e) {
      _notifyIcon.ShowBalloonTip(2000, "提示", "再见！", ToolTipIcon.Info);
      System.Windows.Application.Current.Shutdown();
      Console.WriteLine(@"123456");
    }
    private void DoMinWindow(object sender, System.ComponentModel.CancelEventArgs e) {
      e.Cancel = true;
      Hide();
      _notifyIcon.ShowBalloonTip(2000, "提示", "定时任务运行中！", ToolTipIcon.Info);
    }
  }
}